<template>
  <div class="energy-system-container">
    <!-- Fuel Costs Section -->
    <div class="section">
      <h2 class="title">燃料成本</h2>
      <div class="grid">
        <div class="card">
          <label class="card-label">天然气</label>
          <div class="card-content">
            <input type="number" v-model="fuelCosts1" class="input small">
            <span class="unit">RMB/M³</span>
          </div>
        </div>

        <div class="card">
          <label class="card-label">氢气</label>
          <div class="card-content">
            <input type="number" v-model="fuelCosts2" class="input small">
            <span class="unit">RMB/M³</span>
          </div>
        </div>

        <div class="card">
          <label class="card-label">电力</label>
          <div class="card-content">
            <input type="number" v-model="fuelCosts3" class="input small">
            <span class="unit">RMB/MW</span>
          </div>
        </div>

        <div class="card">
          <label class="card-label">生物质(湿)</label>
          <div class="card-content">
            <input type="number" v-model="fuelCosts4" class="input small">
            <span class="unit"></span>
          </div>
        </div>

        <div class="card">
          <label class="card-label">生物质(干)</label>
          <div class="card-content">
            <input type="number" v-model="fuelCosts5" class="input small">
            <span class="unit"></span>
          </div>
        </div>

        <div class="card">
          <label class="card-label">煤炭</label>
          <div class="card-content">
            <input type="number" v-model="fuelCosts6" class="input small">
            <span class="unit">RMB/t</span>
          </div>
        </div>
        <button class="distribution-btn emission-submit-btn" @click="submit">
          <span class="btn-text">提交</span>

        </button>
      </div>
    </div>
  </div>
</template>

<script lang="ts" setup>
import { ref, onMounted, type Ref } from 'vue'
import axios from 'axios'
import { ElMessage } from 'element-plus'

const fuelCosts1 = ref('')
const fuelCosts2 = ref('')
const fuelCosts3 = ref('')
const fuelCosts4 = ref('')
const fuelCosts5 = ref('')
const fuelCosts6 = ref('')

const submit = async () => {
  try {
    const payloads = [
      { id: 1, value: parseFloat(fuelCosts1.value) || 0 },
      { id: 2, value: parseFloat(fuelCosts2.value) || 0 },
      { id: 3, value: parseFloat(fuelCosts3.value) || 0 },
      { id: 4, value: parseFloat(fuelCosts4.value) || 0 },
      { id: 5, value: parseFloat(fuelCosts5.value) || 0 },
      { id: 6, value: parseFloat(fuelCosts6.value) || 0 }
    ];

    for (const item of payloads) {
      await axios.put('http://localhost:8080/ranliao', item);
    }

    ElMessage.success('已成功更新到数据库');
  } catch (error) {
    console.error('提交失败:', error);
    ElMessage.error('提交失败，请检查数据格式或网络连接');
  }
};

const fetchAndSet = async (url: string, variable: Ref<string>) => {
  try {
    const response = await axios.get(url)
    variable.value = response.data.value
  } catch {
    ElMessage.error(`加载 ${url} 失败`)
  }
}

onMounted(async () => {
  await fetchAndSet('http://localhost:8080/ranliao/1', fuelCosts1)
  await fetchAndSet('http://localhost:8080/ranliao/2', fuelCosts2)
  await fetchAndSet('http://localhost:8080/ranliao/3', fuelCosts3)
  await fetchAndSet('http://localhost:8080/ranliao/4', fuelCosts4)
  await fetchAndSet('http://localhost:8080/ranliao/5', fuelCosts5)
  await fetchAndSet('http://localhost:8080/ranliao/6', fuelCosts6)
})
</script>

<style scoped>
.submit-emission-container {
  text-align: center;
  margin-top: 24px;
}

.emission-submit-btn {
  background-color: #7c3aed; /* 绿色 */
  padding: 12px 24px;
  border-radius: 10px;
  font-size: 16px;
  font-weight: 600;
  box-shadow: 0 4px 10px rgba(22, 163, 74, 0.2);
}

.emission-submit-btn:hover {
  background-color: #7c3aed;
  box-shadow: 0 6px 16px rgba(22, 163, 74, 0.3);
}
.title {
  color: #4c1d95;
  text-align: center;
  margin-bottom: 32px;
  font-size: 28px;
  font-weight: 700;
  padding-bottom: 16px;
  border-bottom: 2px solid #7c3aed;
  letter-spacing: 0.5px;
}
.energy-system-container {
  background-color: #f8fafc;
  box-shadow: 0 4px 24px rgba(0, 0, 0, 0.06);
  border-radius: 14px;
  padding: 36px;
  max-width: 1200px;
  margin: 0 auto;
  font-family: 'Inter', 'Segoe UI', system-ui, sans-serif;
  color: #1e293b;
}

.section-title {
  color: #4c1d95;
  margin-bottom: 24px;
  font-size: 24px;
  font-weight: 700;
  padding-bottom: 12px;
  border-bottom: 2px solid #7c3aed;
}

.section {
  background-color: white;
  border-radius: 12px;
  padding: 24px;
  margin-bottom: 28px;
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);
}

.equipment-section {
  background-color: #f8fafc;
  border-radius: 12px;
  padding: 20px;
  margin-bottom: 20px;
  border: 1px solid #e2e8f0;
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
  gap: 16px;
}

.card {
  background-color: white;
  padding: 16px;
  border-radius: 8px;
  border-left: 4px solid #7c3aed;
  transition: transform 0.2s, box-shadow 0.2s;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05);
}

.card:hover {
  transform: translateY(-2px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.08);
}

.card-label {
  display: block;
  font-weight: 500;
  color: #475569;
  margin-bottom: 8px;
  font-size: 14px;
}

.card-content {
  display: flex;
  align-items: center;
  gap: 8px;
}

.input {
  padding: 8px 12px;
  border: 1px solid #053676;
  border-radius: 6px;
  width: 100px;
  text-align: right;
  font-size: 14px;
  background: white;
  color: #1e293b;
}

.input.small {
  width: 80px;
}

.input:focus {
  outline: none;
  box-shadow: 0 0 0 2px rgba(124, 58, 237, 0.2);
}

.unit {
  font-weight: 500;
  color: #64748b;
  font-size: 13px;
  white-space: nowrap;
}

.btn {
  background-color: #7c3aed;
  color: white;
  border: none;
  padding: 8px 16px;
  border-radius: 6px;
  cursor: pointer;
  font-weight: 500;
  transition: all 0.2s;
  margin-top: 8px;
  font-size: 13px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

.btn:hover {
  background-color: #6d28d9;
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(124, 58, 237, 0.3);
}

.btn-text {
  font-size: 13px;
  line-height: 1.2;
}

.file-name {
  color: rgba(255, 255, 255, 0.8);
  font-size: 11px;
  font-style: italic;
  margin-top: 2px;
}

.save-btn {
  background-color: #10b981;
  color: white;
  border: none;
  padding: 12px 24px;
  border-radius: 6px;
  cursor: pointer;
  font-weight: 600;
  transition: all 0.2s;
  font-size: 16px;
  width: 100%;
  max-width: 300px;
  margin: 20px auto 0;
  display: block;
}

.save-btn:hover {
  background-color: #059669;
  transform: translateY(-1px);
  box-shadow: 0 2px 8px rgba(16, 185, 129, 0.3);
}

.action-bar {
  text-align: center;
  margin-top: 30px;
}

@media (max-width: 768px) {
  .energy-system-container {
    padding: 20px;
    border-radius: 0;
  }

  .grid {
    grid-template-columns: 1fr;
  }

  .input {
    width: 100%;
  }
}
</style>
